<script setup lang="ts">
import { reactive, ref } from 'vue'
import { ElButton, ElMessageBox } from 'element-plus'
import { ContentWrap } from '@/components/ContentWrap'
import { Search } from '@/components/Search'
import { Dialog } from '@/components/Dialog'
import { Table } from '@/components/Table'
import See from './components/See.vue'
// import dayjs from 'dayjs'
import { sysCompetitionSignupList, sysCompetitionSignupDel } from '@/api/competition/index'
import { CrudSchema, useCrudSchemas } from '@/hooks/web/useCrudSchemas'
import { useTable } from '@/hooks/web/useTableTest'

// const searchRef = ref() // 搜索的标签

// 获取字典
import { useCache } from '@/hooks/web/useCache'
const { wsCache } = useCache()
const dict: any = wsCache.get('dict')
// 字典通过value获取label
const getDictLabel = (type, value) => {
  let data = dict[type].find((item) => {
    return item.value == value
  })
  return data ? data.label : ''
}

const { register, tableObject, methods } = useTable<
  {
    total: number
    list: any[]
  },
  any
>({
  getListApi: sysCompetitionSignupList,
  response: {
    data: 'data',
    count: 'count'
  }
})

const { getList, setSearchParams } = methods

const crudSchemas = reactive<CrudSchema[]>([
  {
    field: 'signup_id',
    label: '主键',
    fixed: 'left',
    width: 60,
    table: {
      show: true
    },
    detail: {
      show: false
    }
    // search: {
    //   show: true
    // }
  },
  {
    field: 'name',
    label: '队长名称',
    width: 150,
    search: {
      show: true
    }
  },
  {
    field: 'sex',
    label: '性别',
    width: 120,
    formatter: (_: Recordable, __: TableColumn, cellValue: number | string) => {
      return getDictLabel('user_sex', cellValue)
    },
    search: {
      component: 'Select',
      componentProps: {
        options: dict.user_sex
      },
      show: true
    }
  },
  {
    field: 'type',
    label: '类型',
    width: 120,
    formatter: (_: Recordable, __: TableColumn, cellValue: number | string) => {
      return getDictLabel('competition_signup_type', cellValue)
    },
    search: {
      component: 'Select',
      componentProps: {
        options: dict.competition_signup_type
      },
      show: true
    }
  },
  {
    field: 'id_type',
    label: '证件类型',
    width: 120,
    formatter: (_: Recordable, __: TableColumn, cellValue: number | string) => {
      return getDictLabel('competition_signup_id_type', cellValue)
    },
    search: {
      component: 'Select',
      componentProps: {
        options: dict.competition_signup_id_type
      },
      show: true
    }
  },
  // {
  //   field: 'org_name',
  //   label: '学校/企业名称',
  //   search: {
  //     show: true
  //   }
  // },
  {
    field: 'mobile',
    label: '手机号码',
    search: {
      show: true
    }
  },
  {
    field: 'email',
    label: '电子邮箱'
  },
  // {
  //   field: 'birth_date',
  //   label: '出生年月',
  //   formatter: (_: Recordable, __: TableColumn, cellValue: number | string) => {
  //     return getBirthDate(cellValue)
  //   },
  //   width: 150
  // },

  // {
  //   field: 'id_card_code',
  //   label: '身份证号',
  //   width: 180
  // },
  {
    field: 'create_time',
    label: '创建时间'
  },

  {
    field: 'action',
    width: 150,
    fixed: 'right',
    label: '操作',
    form: {
      show: false
    },
    detail: {
      show: false
    }
  }
])

const { allSchemas } = useCrudSchemas(crudSchemas)

// 删除
const apiDel = async (id) => {
  let delRes = await sysCompetitionSignupDel({ signup_id: id })
    .catch(() => {})
    .finally(() => {})
  if (delRes) {
    getList()
  }
}

const dialogVisible = ref(false)

const dialogTitle = ref('')

const actionType = ref('')

// const getBirthDate = (date) => {
//   if (date) {
//     return dayjs(date).format('YYYY-MM-DD')
//   }
//   return ''
// }

const action = async (type: string, row: any = {}) => {
  actionType.value = type
  switch (type) {
    case 'edit':
      dialogTitle.value = '编辑'
      tableObject.currentRow = row
      dialogVisible.value = true
      break
    case 'add':
      dialogTitle.value = '新增'
      tableObject.currentRow = {
        is_stop: '0'
      }
      dialogVisible.value = true
      break
    case 'see':
      dialogTitle.value = '查看'
      tableObject.currentRow = row
      dialogVisible.value = true
      break
    case 'del':
      ElMessageBox.confirm('确定要删除这条数据吗？', '删除', {
        confirmButtonText: '确认',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(() => {
          apiDel(row.signup_id)
        })
        .catch(() => {
          console.log('这里是取消confirm弹框')
        })
      break
    default:
      console.log('未知类型')
  }
}

/****************************** 初始化 start ******************************/
const init = async () => {
  // 列表查询
  await getList()
}
init()
/****************************** 初始化 end ******************************/
</script>

<template>
  <ContentWrap>
    <Search
      ref="searchRef"
      :schema="allSchemas.searchSchema"
      @search="setSearchParams"
      @reset="setSearchParams"
    />

    <Table
      v-model:pageSize="tableObject.pageSize"
      v-model:currentPage="tableObject.currentPage"
      :columns="allSchemas.tableColumns"
      :data="tableObject.tableList"
      :loading="tableObject.loading"
      :border="true"
      :pagination="{
        total: tableObject.count
      }"
      @register="register"
      :selection="false"
    >
      <template #action="{ row }">
        <ElButton type="success" @click="action('see', row)"> 查看 </ElButton>
        <!-- <ElButton type="danger" @click="action('del', row)"> 删除 </ElButton> -->
      </template>
    </Table>
  </ContentWrap>

  <Dialog v-model="dialogVisible" :title="dialogTitle">
    <See v-if="actionType == 'see'" :currentRow="tableObject.currentRow" />
    <template #footer>
      <ElButton @click="dialogVisible = false">关闭</ElButton>
    </template>
  </Dialog>
</template>
